﻿#region File Description
///------------------------------------------------------------------
/// Program		  :	Algorithm With Graph
/// Written by	  :	Nguyen Khac Trung, Le Hoang Tuan
/// Email		  :	nkt1321991@gmail.com || hoangtuanuit@gmail.com
/// File Name     : MyPontList.cs
/// Created date  :	10/11/2011
/// Modified by	  :	Nguyen Khac Trung
/// Modified date :	11/11/2011
/// Version		  :	1.0
/// Description	  :	Declare my class Point
///------------------------------------------------------------------
#endregion 

#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Diagnostics;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Threading;
#endregion


namespace Graph
{
    /// <summary>
    /// 
    /// </summary>
    public class MyPointGeometry : Shape
    {

        #region Fields

        //
        private double m_X;
        
        private double m_Y;
        
        private EllipseGeometry m_myPoint;

        #endregion

        #region Properties
        public double X
        {
            get { return m_X; }
            set { m_X = value; }
        }


        public double Y
        {
            get { return m_Y; }
            set { m_Y = value; }
        }


        public EllipseGeometry MYPOINT
        {
            get { return m_myPoint; }
            set { m_myPoint = value; }
        }
        #endregion
        
        #region Process
        protected override Geometry DefiningGeometry
        {
            get
            {
                m_myPoint = new EllipseGeometry(new Point(m_X, m_Y), 10, 10);
                m_myPoint.Freeze();
                return m_myPoint;
            }
        }
        #endregion
        
    }

    /// <summary>
    /// 
    /// </summary>
    public class MyPoint
    {

        #region Fields

        private MyPointGeometry m_ell;

        #endregion

        #region Properties

        public MyPointGeometry ELL
        {
            get { return m_ell; }
            set { m_ell = value; }
        }

        #endregion

        #region Initialization

        public MyPoint(double _X, double _Y, String _str)
        {
            m_ell = new MyPointGeometry();
            m_ell.Name = _str;
            m_ell.X = _X;
            m_ell.Y = _Y;
            m_ell.Stroke = Brushes.Black;
            m_ell.StrokeThickness = 2;
            m_ell.Fill = Brushes.White;
        }

        #endregion

    }

    /// <summary>
    /// 
    /// </summary>
    public class MyPointList
    {

        #region Fields

        private List<MyPoint> m_listMyPoint;

        private IInputElement m_iInputElement;

        

        #endregion 

        #region Properties

        public List<MyPoint> LISTMYPOINT
        {
            get { return m_listMyPoint; }
            set { m_listMyPoint = value; }
        }

        #endregion

        #region Initialization

        public MyPointList(IInputElement _iInPutElement)
        {
            m_listMyPoint = new List<MyPoint>();
            m_iInputElement = _iInPutElement;
        }

#endregion

        #region Process

        public int SearchIndex(String _name)
        {   
            int index = 0;
            foreach (MyPoint point in m_listMyPoint)
            {
                if (point.ELL.Name == _name)
                {
                    return index;
                }
                ++index;
            }
            return -1;
        }

        public int IsClickMyPoint(int delta)
        {
            int index = 0;
            Point currentPositionMouse = Mouse.GetPosition(m_iInputElement);

            foreach (MyPoint testPoint in m_listMyPoint)
            {
                if (currentPositionMouse.X >= (testPoint.ELL.X - delta) &&
                    currentPositionMouse.X <= (testPoint.ELL.X + delta) &&
                    currentPositionMouse.Y >= (testPoint.ELL.Y - delta) &&
                    currentPositionMouse.Y <= (testPoint.ELL.Y + delta))
                {
                    return index;
                }
                ++index;
            }
            return -1;
        }

        public MyPointGeometry AddPointToPointList(bool? _isCheckedBoxNamePoint)
        {
            Point currentPositionMouse = Mouse.GetPosition(m_iInputElement);
            if (IsClickMyPoint(50) >= 0)
            {
                return null;
            }

            String nametemp = null;

            if (_isCheckedBoxNamePoint == false)
            {
                WindowSelectNamePoint window = new WindowSelectNamePoint();
                window.ShowDialog();
                nametemp = window.Tag.ToString();

                if (nametemp == "none")
                {
                    return null;
                }

                foreach (MyPoint temp in m_listMyPoint)
                {
                    if (temp.ELL.Name == nametemp)
                    {
                        MessageBox.Show("XIN LỖI! TÊN ĐIỂM BẠN CHỌN ĐÃ CÓ!!");
                        return null;
                    }
                }
            }
            else
            {
                bool i = false;
                nametemp = "A";
                do
                {
                    i = true;
                    foreach (MyPoint temp in m_listMyPoint)
                    {
                        if (nametemp == temp.ELL.Name)
                        {
                            i = false;
                            break;
                        }
                    }

                    if (nametemp == "[")
                    {
                        MessageBox.Show("XIN LỖI! ĐÃ CUNG CẤP ĐỦ TÊN CHO CHƯƠNG TRÌNH");
                        return null;
                    }

                    if (i == false)
                    {
                        nametemp = ((char)((int)((char)nametemp[0]) + 1)).ToString();
                    }
                } while (!i);
            }

            MyPoint mypoint = new MyPoint(currentPositionMouse.X, currentPositionMouse.Y, nametemp);
            m_listMyPoint.Add(mypoint);

            return mypoint.ELL;
        }

        public String RemovePointToPointList()
        {
            int index = IsClickMyPoint(10);
            if (index >= 0)
            {
                String str = m_listMyPoint[index].ELL.Name;
                return str;
            }

            return null;
        }

        public void showEffect(int _index)
        {
            m_listMyPoint[_index].ELL.Stroke = Brushes.Red;
            m_listMyPoint[_index].ELL.StrokeThickness = 10;
        }

        public void resetColorStroke()
        {
            foreach (MyPoint point in m_listMyPoint)
            {
                point.ELL.Stroke = Brushes.Black;
                point.ELL.StrokeThickness = 2;
            }
        }

        #endregion

    }
}
